package com.itheima.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.serivce.CategoryService;
import com.itheima.serivce.DishService;
import com.itheima.serivce.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    //根据id删除  删除之前需要根据id判断

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, id);
        //          查询结果是否包含
        int count = dishService.count(wrapper);
        //查看当前分类是否关联了菜品,如已关联 抛出异常
        if (count > 0) {
            throw new CustomException("当前分类下关联菜品,不能被删除");
        }

        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Setmeal::getCategoryId, id);
        //           查询结果是否包含
        int count1 = setmealService.count(wrapper1);
        //查看当前分类是否包含了套餐  如已关联,抛出异常
        if (count1 > 0) {
            throw new CustomException("当前分类下关联套餐,不能被删除");
        }

        //正常情况删除
        super.removeById(id);

    }

}
